
#include "SmurfsPlane.h"

/******************************************************************************************
* Default Constructor
******************************************************************************************/
SmurfsPlane::SmurfsPlane(void) {
	
}

/******************************************************************************************
* Constructor
******************************************************************************************/
SmurfsPlane::SmurfsPlane(Vector3 pointA, Vector3 pointB, Vector3 pointC) {
	normal = (pointB - pointA).cross(pointC - pointA).getNormalized();
	dot = -normal.dot(pointA);
}

/**************************************************************************************************
* Alternate constructor
**************************************************************************************************/
SmurfsPlane::SmurfsPlane(Vector3 origin, Vector3 normal) {
	this->normal = normal;
	dot = -normal.dot(origin);
}

/******************************************************************************************
* Destructor
******************************************************************************************/
SmurfsPlane::~SmurfsPlane(void) { }

/******************************************************************************************
* Creates a plane from a polygon
******************************************************************************************/
SmurfsPlane SmurfsPlane::getPlaneFromPolygon(SmurfsPolygon* polygon) {
	return SmurfsPlane(polygon->getPoints()[0], polygon->getPoints()[1], polygon->getPoints()[2]);
}

/******************************************************************************************
* Checks if edge defined by a and be
******************************************************************************************/
int SmurfsPlane::intersectEdgeAgainstPlane(Vector3 a, Vector3 b, SmurfsPlane plane, 
	float& intPer, Vector3& intersect) { 
	
	Vector3 ba = b - a;
	intPer = (plane.getDot() - plane.getNormal()->dot(a)) / plane.getNormal()->dot(ba);

	if(intPer >= 0.0f && intPer <= 1.0f) {
		intersect = a + (ba * intPer);
		return 1;
	}

	return 0;
}

/**************************************************************************************************
* Find the distance to the plane (signed to see in front or in back).
**************************************************************************************************/
float SmurfsPlane::signedDistanceTo(Vector3 point) {
	return (normal.dot(point)) + dot;
} 

/******************************************************************************************
* Returns the plane's normal
******************************************************************************************/
Vector3* SmurfsPlane::getNormal(void) { return &normal; }

/******************************************************************************************
* Returns the dot product of the normal with a point on the plane
******************************************************************************************/
float SmurfsPlane::getDot(void) { return dot; }